WIFI_HI3881_SITE_METHOD = git
WIFI_HI3881_SITE = https://github.com/themactep/wifi-hi3881
WIFI_HI3881_SITE_BRANCH = master
WIFI_HI3881_VERSION = 867943faad79bb80d83739c731cf7dbac6f8d644
# $(shell git ls-remote $(WIFI_HI3881_SITE) $(WIFI_HI3881_SITE_BRANCH) | head -1 | cut -f1)

WIFI_HI3881_LICENSE = GPL-2.0

HI3881_MODULE_NAME = hi3881

WIFI_HI3881_MODULE_MAKE_OPTS = \
	KDIR=$(LINUX_DIR) \
	ARCH=$(KERNEL_ARCH) \
	CROSS_COMPILE=$(TARGET_CROSS) \
	LDFLAGS="$(TARGET_LDFLAGS)"

define WIFI_HI3881_LINUX_CONFIG_FIXUPS
	$(call KCONFIG_ENABLE_OPT,CONFIG_WLAN)
	$(call KCONFIG_ENABLE_OPT,CONFIG_WIRELESS)
	$(call KCONFIG_ENABLE_OPT,CONFIG_WIRELESS_EXT)
	$(call KCONFIG_ENABLE_OPT,CONFIG_WEXT_CORE)
	$(call KCONFIG_ENABLE_OPT,CONFIG_WEXT_PROC)
	$(call KCONFIG_ENABLE_OPT,CONFIG_WEXT_PRIV)
	$(call KCONFIG_SET_OPT,CONFIG_CFG80211,y)
	$(call KCONFIG_SET_OPT,CONFIG_MAC80211,y)
	$(call KCONFIG_ENABLE_OPT,CONFIG_MAC80211_RC_MINSTREL)
	$(call KCONFIG_ENABLE_OPT,CONFIG_MAC80211_RC_MINSTREL_HT)
	$(call KCONFIG_ENABLE_OPT,CONFIG_MAC80211_RC_DEFAULT_MINSTREL)
	$(call KCONFIG_SET_OPT,CONFIG_MAC80211_RC_DEFAULT,"minstrel_ht")
endef

define WIFI_HI3881_BUILD_CMDS
	$(TARGET_MAKE_ENV) $(MAKE) $(WIFI_HI3881_MODULE_MAKE_OPTS) linux_driver -C $(@D)
endef

LINUX_CONFIG_LOCALVERSION = $(shell awk -F "=" '/^CONFIG_LOCALVERSION=/ {print $$2}' $(BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE))

define WIFI_HI3881_INSTALL_CONFIGS
	$(INSTALL) -m 0755 -d $(TARGET_DIR)/lib/modules/$(FULL_KERNEL_VERSION)$(call qstrip,$(LINUX_CONFIG_LOCALVERSION))
	touch $(TARGET_DIR)/lib/modules/$(FULL_KERNEL_VERSION)$(call qstrip,$(LINUX_CONFIG_LOCALVERSION))/modules.builtin.modinfo

	$(INSTALL) -D -m 0644 $(@D)/firmware/wifi_cfg/fcc/wifi_cfg \
		$(TARGET_DIR)/usr/share/wifi/wifi_cfg

	$(INSTALL) -D -m 0644 $(@D)/firmware/hi3881_fw.bin \
		$(TARGET_DIR)/lib/firmware/hi3881_fw.bin

	sed -i 's|ADDR_FILE_FILES=1,0xe4800,/vendor/firmware/hisilicon/hi3881_fw.bin;|ADDR_FILE_FILES=1,0xe4800,/lib/firmware/hi3881_fw.bin;|' $(TARGET_DIR)/usr/share/wifi/wifi_cfg
endef

WIFI_HI3881_POST_INSTALL_TARGET_HOOKS += WIFI_HI3881_INSTALL_CONFIGS

define WIFI_HI3881_INSTALL_TARGET_CMDS
	$(INSTALL) -D -m 0755 $(@D)/driver/$(HI3881_MODULE_NAME).ko \
		$(TARGET_DIR)/lib/modules/$(FULL_KERNEL_VERSION)$(call qstrip,$(LINUX_CONFIG_LOCALVERSION))/extra/$(HI3881_MODULE_NAME).ko
endef

$(eval $(kernel-module))
$(eval $(generic-package))
